<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class ComtController extends Controller
{
    //
    public function index(Request $request)
    {
    	//保存搜索条件
    	$where = [];
    	//实例化要操作的表
        $db = DB::table("comment")
            ->join('goods','comment.gid','=','goods.id')
            ->join('user','comment.uid','=','user.id')
            ->select('comment.*','goods.goodname','user.username');
    	//判断有没有要搜索的物品id字段的值
    	if($request->has("goodname")){
    		//获取要搜索的name字段名
    		$goodname=$request->input("goodname");
    		//添加where模糊匹配条件
    		$db->where("goodname","like","%{$goodname}%");
    		$where["goodname"] = $goodname;
    	}
    	//分页
    	$list= $db->paginate(9);
    	//加载模板
    	return view("admin.comment.comt",["list"=>$list,"where"=>$where]);

    }
    public function show($id)
    {
        $data=DB::table('comment')->where('id',$id)->first();
        return view('admin.comment.detail',['data'=>$data]);
    }
    public function destroy($id)
    {
    	$row = DB::table("comment")->where("id",$id)->delete();
    	if($row>0){
    		return redirect("admin/comt");
    	}
    }
    public function edit($id)
    {
        return view("admin.comment.reply",["id"=>$id]);
    }
    public function store(Request $request)
    {
        $data = $request->except("_token");
        $row = DB::table("reply")->insertGetId($data);
        if($row>0){
            $state=array('state'=>'1');
            $id=DB::table('reply')->where('id',$row)->first()->cid;
            $row2=DB::table('comment')->where('id',$id)->update($state);
            return redirect("admin/comt");
        }
    }
}
